package DataCollect;

import com.google.protobuf.ByteString;
import dms.MATPOuterClass;
import dms.MTransfCtrlOuterClass;
import org.zeromq.ZMQ;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.UUID;

public class MakeData {
    public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
        ZMQ.Context zmq = ZMQ.context(1);

        ZMQ.Socket pub = zmq.socket(ZMQ.PUB);

        pub.bind("tcp://localhost:5555");

//        int i = 0;
        while (true){
            MTransfCtrlOuterClass.MTransfCtrl.Builder builder = MTransfCtrlOuterClass.MTransfCtrl.newBuilder();

            builder.setMsgtype(1);

            MATPOuterClass.MATP.Builder matpBulder = MATPOuterClass.MATP.newBuilder();

            MATPOuterClass.MPacketHead.Builder mPacketHeadBuilder = MATPOuterClass.MPacketHead.newBuilder();

            mPacketHeadBuilder.setATPType(1);
            Random trainID = new Random();
            mPacketHeadBuilder.setTrainID(trainID.nextInt(5000));
            mPacketHeadBuilder.setTrainNum(ByteString.copyFrom("G201","utf-8"));
            mPacketHeadBuilder.setAttachRWBureau(1);
            mPacketHeadBuilder.setViaRWBureau(2);
            mPacketHeadBuilder.setCrossDayTrainNum(true);
            mPacketHeadBuilder.setDriverID(ByteString.copyFrom("D001","utf-8"));

            matpBulder.setPacketHead(mPacketHeadBuilder);

            MATPOuterClass.MATPBaseInfo.Builder matpBaseInfoBuilder = MATPOuterClass.MATPBaseInfo.newBuilder();



            matpBaseInfoBuilder.setDataTime(2);
            matpBaseInfoBuilder.setSpeed(100);
            matpBaseInfoBuilder.setLevel(1);

            matpBulder.setATPBaseInfo(matpBaseInfoBuilder);

            MATPOuterClass.MSignal.Builder matpSingle = MATPOuterClass.MSignal.newBuilder();
            matpSingle.setLongitude(119);
            matpSingle.setLatitude(36);

            matpBulder.setSignal(matpSingle);

            matpBulder.setUUID(ByteString.copyFrom(UUID.randomUUID().toString(),"utf-8"));

            builder.setData(matpBulder.build().toByteString());

            MTransfCtrlOuterClass.MTransfCtrl build = builder.build();

            pub.send(build.toByteArray());

            Thread.sleep(500);

        }

//        pub.close();
//        zmq.close();
    }
}
